
const Cesium = window.Cesium;

/** @type {Object} 基础颜色信息*/
const COLOR = {
    DEFAULT: Cesium.Color.fromCssColorString("#0000FF"),
    RED: Cesium.Color.fromCssColorString('#B22222'),
    BLUE: Cesium.Color.fromCssColorString('#0000FF'),
    GREEN: Cesium.Color.fromCssColorString('#2E8B57'),
    YELLOW: Cesium.Color.fromCssColorString('#FFEC3D'),
    WHITE: Cesium.Color.fromCssColorString('#FFFFFF'),
    ORANGE: Cesium.Color.fromCssColorString('#FF6600'),
    DARK_GREEN: Cesium.Color.fromCssColorString('#213028'),
    LIGHT_BLUE: Cesium.Color.fromCssColorString('#19FFD8'),

    //解调
    'DEMODULATIONCOVER': Cesium.Color.fromCssColorString("#FF363B"),
    //定位
    'LOCATIONCOVER': Cesium.Color.fromCssColorString("#FAB318"),
    //截获
    'INTERCEPTCOVER': Cesium.Color.fromCssColorString("#FFEC3D"),

    // 地图遮罩层
    MarkViewColor: Cesium.Color.fromCssColorString("#19FFD85A"),
}

/**
 * 基础颜色信息
 *
 * @export
 */
export { COLOR }

/**
 * 格式化颜色信息
 *
 * @param {String} color 颜色信息
 * @return {Cesium.Color} 地图颜色类型
 */
export function formarColor(color) {
    if (!color) {
        return false;
    };
    color = color.toLocaleUpperCase();
    if (COLOR.hasOwnProperty(color)) {
        return COLOR[color];
    } else if (color.indexOf('#') == 0) {
        return Cesium.Color.fromCssColorString(color);
    } else {
        return false;
    }
}


/**
 * 获取两个坐标中点
 *
 * @param {Array} positions 点集合（Cartesian3）
 * @return {Cartesian3} 
 */
const GetCenter = function (positions) {
    var Center = Cesium.BoundingSphere.fromPoints(positions).center;
    var _positions1 = Cesium.Cartographic.fromCartesian(positions[0]);
    var _positions2 = Cesium.Cartographic.fromCartesian(positions[1])
    var _Center = Cesium.Cartographic.fromCartesian(Center)
    _Center.latitude = Cesium.Math.toDegrees(_Center.latitude);
    _Center.longitude = Cesium.Math.toDegrees(_Center.longitude);
    _Center.height = (_positions1.height + _positions2.height) / 2;
    return Cesium.Cartesian3.fromDegrees(_Center.longitude, _Center.latitude, _Center.height)
};

/**
 * 经纬度坐标信息
 *
 * @param {Object} Position 经纬度坐标信息
 * @return {Cesium.Cartesian3} 地图的 Cartesian3 类型坐标
 */
const CreatePosition = function (Position) {
    return new Cesium.Cartesian3.fromDegrees(Position.longitude, Position.latitude, Position.height || 5000)
}


/**
 * 判断朝向
 *
 * @param {Cesium.Cartesian3} pointA 起始点
 * @param {Cesium.Cartesian3} pointB 结束点
 * @return {Number} 角度
 * @memberof Entity
 */
const GetHeading = function (pointA, pointB) {
    const transform = Cesium.Transforms.eastNorthUpToFixedFrame(pointA);
    const positionvector = Cesium.Cartesian3.subtract(pointB, pointA, new Cesium.Cartesian3());
    const vector = Cesium.Matrix4.multiplyByPointAsVector(Cesium.Matrix4.inverse(transform, new Cesium.Matrix4()), positionvector, new Cesium.Cartesian3());
    const direction = Cesium.Cartesian3.normalize(vector, new Cesium.Cartesian3());
    const heading = Math.atan2(direction.y, direction.x) - Cesium.Math.PI_OVER_TWO;
    return Cesium.Math.TWO_PI - Cesium.Math.zeroToTwoPi(heading);
}

/**
 * 改变转换角度
 * 
 * @param {Object} options 经纬度对象
 * @memberof Entity
 */
const GetRotation = function (newPoint, oldPoint) {
    let lg = newPoint.longitude;
    let la = newPoint.latitude;
    if (lg && la && oldPoint.longitude && oldPoint.latitude && oldPoint.longitude != lg && oldPoint.latitude != la) {
        try {
            //方位角
            var d1 = Cesium.Cartesian3.fromDegrees(lg, la);
            var d2 = Cesium.Cartesian3.fromDegrees(oldPoint.longitude, oldPoint.latitude);
            var rotation = GetHeading(d2, d1)
            return -rotation;
        } catch (error) {
            console.log(error, lg, la, oldPoint.longitude, oldPoint.latitude);
        }
    }
}



/**
 * 返回两点之间距离
 *
 * @param {Cesium.Cartesian3} a
 * @param {Cesium.Cartesian3} b
 * @return {Number} 
 */
const distance = function (a, b) {
    return Cesium.Cartesian3.distance(a, b);
}

/**
 * 返回多个点之间距离
 *
 * @param {Array} pointArray 点集合(Cesium.Cartesian3)
 * @return {Number} 
 */
const distanceArray = function (pointArray) {
    let M = 0;
    const length = pointArray.length;
    for (let i = 1; i < length; i++) {
        const a = pointArray[i];
        const b = pointArray[i - 1];
        M += distance(b, a);
    };
    return (M / 1000).toFixed(2);
}


/**
 * 返回最后连点之间距离
 *
 * @param {Array} pointArray 点集合(Cesium.Cartesian3)
 * @return {Number} 
 */
const distanceLast = function (pointArray) {
    const M = distance(pointArray[pointArray.length - 3], pointArray[pointArray.length - 2]);
    return (M / 1000).toFixed(2);
};



export {
    GetCenter,
    CreatePosition,
    GetHeading,
    GetRotation,
    distance,
    distanceArray,
    distanceLast,
}


